package com.mxpio.webapp.controller;

import com.mxpio.webapp.entity.MonitorNet;
import com.mxpio.webapp.service.IMonitorNetService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * 网络
 *
 * @author: Cheng
 * @date: 2022/11/06
 */
@Slf4j
@Tag(name = "MonitorNetController", description = "网络信息")
@RestController()
@RequestMapping("/monitor-network")
public class MonitorNetController {

    @Autowired
    private IMonitorNetService monitorNetService;

    @GetMapping("/list")
    @Operation(summary = "网络", description = "根据过滤字段filter获取网络列表列表", method = "GET")
    public Result<Page<MonitorNet>> list(Criteria criteria,
                                         @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                         @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo) throws Exception {
        Pageable pageAble = PageRequest.of(pageNo - 1, pageSize);
        Page<MonitorNet> page = monitorNetService.queryAll(criteria, pageAble);
        return Result.OK(page);
    }

    @PostMapping("/add")
    @Operation(summary = "添加网络信息", description = "添加网络信息", method = "POST")
    public Result<MonitorNet> add(@RequestBody MonitorNet monitorNet) throws Exception {
        monitorNetService.create(monitorNet);
        return Result.OK("添加网络信息成功",monitorNet);
    }

    @PutMapping("/edit")
    @Operation(summary = "更新网络信息", description = "更新网络信息", method = "PUT")
    public Result<MonitorNet> edit(@RequestBody MonitorNet monitorNet) throws Exception {
        monitorNetService.editMonitorNet(monitorNet);
        return Result.OK("编辑成功",null);
    }

    @DeleteMapping("/delete/{ids}")
    @Operation(summary = "删除网络信息", description = "删除网络信息", method = "DELETE")
    public Result<MonitorNet> delete(@PathVariable(name = "ids", required = true) String ids) throws Exception {
        String[] idList = ids.split(",");
        monitorNetService.delete(Arrays.asList(idList));
        return Result.OK("删除网络信息",null);
    }



}
